package com.molan.molan_music.controller;

import com.molan.molan_music.dto.UserCreateRequest;
import com.molan.molan_music.dto.UserDto;
import com.molan.molan_music.dto.UserUpdateRequest;
import com.molan.molan_music.mapper.UserMapper;
import com.molan.molan_music.service.UserService;
import com.molan.molan_music.vo.UserVo;
import com.molan.molan_music.vo.pageVo.PageRequestVo;
import com.molan.molan_music.vo.pageVo.PageResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Pageable;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    @ApiOperation(value = "获取所有的用户",notes = "获取所有的用户")
    @RolesAllowed({"ROLE_ADMIN"})
    @PostMapping("/list")
    PageResultVo search(@RequestBody PageRequestVo pageRequestVo){

        return userService.search(pageRequestVo);
    }

    @ApiOperation(value = "新增用户",notes = "新增用户")
    @RolesAllowed({"ROLE_ADMIN"})
    @PostMapping("/addUser")
    UserVo addUser(@Validated @RequestBody UserCreateRequest userCreateDto){
        return userMapper.toVo(userService.addUser(userCreateDto));
    }

    @ApiOperation(value = "根据id获取用户",notes = "根据id获取用户")
    @GetMapping("/getUserById/{id}")
    UserVo getUserById(@PathVariable String id){
        return userMapper.toVo(userService.getUserById(id));
    }

    @ApiOperation(value = "根据id更新用户",notes = "根据id更新用户")
    @RolesAllowed({"ROLE_ADMIN"})
    @PostMapping("/updateUser/{id}")
    UserVo updateUser(@PathVariable String id,
                      @Validated @RequestBody UserUpdateRequest userUpdateRequest){
        return userMapper.toVo(userService.updateUser(id,userUpdateRequest));
    }

    @ApiOperation(value = "根据id删除用户",notes = "根据id删除用户")
    @RolesAllowed({"ROLE_ADMIN"})
    @PostMapping("/deleteUser/{id}")
    int deleteUser(@PathVariable String id){
        return userService.deleteUser(id);
    }


    @ApiOperation(value = "获取当前用户",notes = "获取当前用户")
    @GetMapping("/me")
    UserVo currentUser(){
        return userMapper.toVo(userService.getCurrentUser());
    }
}
